/**
 * 鱼塘协议
 */
class FishPoolsPrase{
    public constructor() {
	}
    private static _instance:FishPoolsPrase;

    public static instance():FishPoolsPrase{
		if(!FishPoolsPrase._instance){
			FishPoolsPrase._instance = new FishPoolsPrase();
		}
		return FishPoolsPrase._instance;
	}

    //买鱼
    public buyFish(parma, call?:Function,scope?){
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version,
            fish_id:parma.id
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/buy_fish' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                MessageUtil.tips('购买成功');
                RoleCache.instance().data.money = data.data.money;
                RoleCache.instance().fishIncome = data.data.fishpond_income;
                GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGED);
 
                if(call)
                {
                    let fish = {
                        fish_id:parma.id,
                        createtime:data.data.createtime
                    }
                    call.call(scope, fish);
                }
            }
            else
            {
                MessageUtil.tips('购买失败');
            }
        })
    }

    //获取已经拥有的鱼
    public getFish(call?:Function,scope?)
    {
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/get_has_fish' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(call)
            {
                call.call(scope, data.data);
            }
        })
    }

    //卖鱼
    public soleFish(parma, call?:Function,scope?)
    {
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version,
            fish_id:parma.fish_id,
            createtime:parma.createtime
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/sale_fish' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(call)
            {
                MessageUtil.tips('售出成功');
                RoleCache.instance().fishIncome = data.data.fishpond_income;
                RoleCache.instance().data.money = data.data.money;
                GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGED);
                call.call(scope);
            }
        });
    }

    //增加鱼塘容量
    public addFishpondCnt()
    {
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/update_pond_count' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            MessageUtil.tips('鱼塘容量变大了!')
            RoleCache.instance().addFishpondCnt();
            let logic:FishPoolsUI = UIManager.instance().getLogicByName(UIConst.FISH_POOLS) as FishPoolsUI;
            if(logic)
            {
                logic.updateFishCnt();
                logic.initLevelUpGroup();
            }
        })
    }

    //聚宝盆
    public getMoney(parma)
    {
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version,
            hours:parma.hours
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/get_hours_money' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                RoleCache.instance().data.money = data.data.money;
                GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGED);
                MessageUtil.tips('聚宝' + parma.hours + 'h收益成功!');
            }
            else
            {
                MessageUtil.tips('聚宝失败');
            }
        })
    }

    //收益系数
    public getIncome(call:Function,scope)
    {
        UIManager.instance().showNormalLoading();
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : 'mnq';
        let data = {
            openid : openid,
            version:GameConst.version
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishpond/add_fishpond_rate' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                RoleCache.instance().fishpond_rate = data.data.fishpond_rate;
                RoleCache.instance().fishIncome = data.data.fishpond_income;
                MessageUtil.tips('升级成功!');
                if(call)
                {
                    call.call(scope);
                }
            }
            else
            {
                MessageUtil.tips('升级失败!');
            }
        })
    }
}